package com.easepal802s.project.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.interfaces.IProMasaChoose;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 人群专属
 * Created by FHB on 2018/3/26.
 */

public class LayoutPMThree extends RelativeLayout implements View.OnClickListener{
    private ImageView mIvBeauty;//女王美体
    private ImageView mIvMan;//男士健身
    private ImageView mIvOlder;//老年模式
    private List<ImageView> mViewList = new ArrayList<>();
    private IProMasaChoose mAction;
    private MyHandler myHandler;
    public LayoutPMThree(Context context) {
        super(context);
    }

    public LayoutPMThree(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void setAction(IProMasaChoose action){
        mAction = action;
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        myHandler = new MyHandler(this);
        mIvBeauty =  findViewById(R.id.zy_masa_iv_beauty);
        mIvMan =  findViewById(R.id.zy_masa_iv_man);
        mIvOlder =  findViewById(R.id.zy_masa_iv_old);
        mViewList.add(mIvBeauty);
        mViewList.add(mIvMan);
        mViewList.add(mIvOlder);
        mIvBeauty.setOnClickListener(this);
        mIvMan.setOnClickListener(this);
        mIvOlder.setOnClickListener(this);


    }

    public void setSelect(int position) {
        myHandler.obtainMessage(100,position).sendToTarget();
    }
    private void makeChoose(int position){
        if (position == -1) {
            for (int i = 0; i < mViewList.size(); i++) {
                mViewList.get(i).setSelected(false);
            }
        } else {
            for (int i = 0; i < mViewList.size(); i++) {
                if (i == position) {
                    Log.e("getDataPer2","select"+position);
                    mViewList.get(i).setSelected(true);
                } else {

                    mViewList.get(i).setSelected(false);
                }

            }
        }
    }
    private  class MyHandler extends Handler {
        WeakReference<LayoutPMThree> mMainActivityWeakReference;

        MyHandler(LayoutPMThree layoutOne) {
            mMainActivityWeakReference = new WeakReference<>(layoutOne);
        }

        @Override
        public void handleMessage(Message msg) {
            final LayoutPMThree mLayoutTwo= mMainActivityWeakReference.get();
            int position = (int)msg.obj;
            Log.e("getDataPer","select"+position);
            if (mLayoutTwo != null) {

                makeChoose(position);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zy_masa_iv_beauty:
                mAction.chooseMassage("女王美体", BleConstant.NVWMT);
                break;
            case R.id.zy_masa_iv_man:
                mAction.chooseMassage("男士健体",BleConstant.NANSJT);
                break;
            case R.id.zy_masa_iv_old:
                mAction.chooseMassage("老年模式",BleConstant.LAONMS);
                break;

        }
    }
}
